<?php
namespace Ucmaim\Controller\Admin;
use Think\Controller;
use \Pinyin;

class CategoryController extends CommonController {

    public function _initialize(){
        parent::_initialize();
    }

    public function category(){
        $do = I('get.do');
        switch($do){
            case 'add':
                $this->_categoryAdd();
                break;
            case 'edit':
                $this->_categoryEdit();
                break;
            case 'del':
                $this->_categoryDel();
                break;
            case 'check':
                $this->_categoryCheck();
                break;
            default:
                $cate=M('goods_category');
                $categorys=$cate->where('gc_pid=0')->order("gc_sort desc")->select();
                $this->assign('list',$categorys);

                $this->display('Admin/Category/categoryList');
        }
    }
    private function _categoryAdd(){
        if( IS_GET ){
            $id=I('get.gc_id');
            if($id!=''){
                $category=M('goods_category')->where("gc_id=$id")->order("gc_sort asc")->find();
	            $result = D('GoodsCategory')->getRelation($category);
                $this->assign('lanmu',$category);
                $this->assign('result',$result);
            }
            $this->display('Admin/Category/categoryOperation');
        }else{
            $data['gc_name']=I('post.gc_name');
            $data['gc_pid']=I('lanmu','','intval');
            if($_FILES['photo']['name']){
                $config = array(
                    'maxSize'    =>    3145728,
                    'rootPath'   =>    './Application/Ucmaim/Uploads/',
                    'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                    'subName'    =>    array('date','Ymd'),
                );
                $upload = new \Think\Upload($config);
                $info   =   $upload->upload();
                foreach($info as $v){
                    $pitcure = $v['savepath'].$v['savename'];
                }
                $data['gc_pic']=$pitcure;
            }
            $firstname=I('post.firstname');
            if($firstname==''){
                import('Org.Util.Pinyin');
                $Pinyin = new Pinyin();
                $name=I('post.gc_name');
                $str=$Pinyin::encode("$name",1,2);
                $data['gc_firstname']=strtoupper(substr($str,0,1));
            }else{
                $data['gc_firstname']=$firstname;
            }
            $relation=I('post.relation');
            if($relation!=''){
                $data['gc_relation']=I('post.relation').',';
            }
            $category=M('goods_category');
	        $sort=(int)I('post.sort');
	        if(is_numeric($sort)){
		        $data['gc_sort']=$sort;
	        }else{
		        $this->error("排序必须是数字");
	        }
            $mark=$category->add($data);
            if($mark){
                $this->redirect('Admin/Category/category');
            }else{
                $this->error('添加失败');
            }

        }


    }
    private function _categoryEdit(){
        if( IS_GET ){
            $id=I('get.id');
            $category=M('goods_category');

            $cate=$category->where("gc_id=$id")->find();
            $this->assign("cate",$cate);
	        $results = D('GoodsCategory')->getRelation($cate);
	        $this->assign('result',$results);
            $this->id=$id;
            $this->display('categoryOperation');
        }else{
            $id=I('post.id');
            $name=I('post.gc_name');
            if($name==''){
                $this->error('请填写分类名');
            }
            $data['gc_name']=$name;
            import('Org.Util.Pinyin');
            $Pinyin = new Pinyin();
            $name=I('post.gc_name');
            $str=$Pinyin::encode("$name",1,2);
            $data['gc_firstname']=strtoupper(substr($str,0,1));
            if($_FILES['photo']['name']){
                $config = array(
                    'maxSize'    =>    3145728,
                    'rootPath'   =>    './Application/Ucmaim/Uploads/',
                    'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                    'subName'    =>    array('date','Ymd'),
                );
                $upload = new \Think\Upload($config);
                $info   =   $upload->upload();
                foreach($info as $v){
                    $pitcure = $v['savepath'].$v['savename'];
                }
                $data['gc_pic']=$pitcure;
            }
	        $sort=I('post.sort');
	        if(is_numeric($sort)){
		        $data['gc_sort']=$sort;
	        }else{
		        $this->error("排序必须是数字");
	        }
            $category=M('goods_category');
            $mark=$category->where("gc_id=$id")->save($data);
            if($mark){
                $this->redirect('Admin/Category/category');
            }else{
                $this->error('修改失败');
            }
        }

    }
    private function _categoryDel(){
        $gc_id=I('get.id');
        $category=M('goods_category');
        $mark=$category->where("gc_pid=$gc_id")->find();
        if($mark){
            $this->error('请删除子分类');
        }
        $mark=$category->where("gc_id=$gc_id")->delete();
        if($mark){
            $this->redirect('Admin/Category/category');
        }else{
            $this->error('删除失败');
        }
    }
    private function _categoryCheck(){
        $gift=I('get.gift');
        $id=I('get.id');
        $this->cateName=M('goods_category')->where("gc_pid=$id")->order("gc_sort desc")->find();
        $categorys=M('goods_category')->where("gc_pid=$id")->select();
        $this->assign('list',$categorys);
        if($categorys){
            foreach($categorys as $v){
                $gx[]=$v['gc_relation'];
            }
            if($gx){
                $arr=explode(',',$gx[0]);
                $map['gc_id']  = array('in',$arr);
                $relations=M('goods_category')->where($map)->field('gc_name')->select();
	            $rela='';
                foreach($relations as $v){
                    $rela.=$v['gc_name'].'>';
                }
                $this->assign("rela",$rela);
            }
        }
        if($gift==1){
            $this->display('categoryGSonList');
        }else{
            $this->display('categorySonList');
        }
    }

    public function stdmode(){
        $do = I('get.do');
        switch($do){
            case 'add':
                $this->_stdmodeAdd();
                break;
            case 'edit':
                $this->_stdmodeEdit();
                break;
            case 'del':
                $this->_stdmodeDel();
                break;
            default:
                $type=M('goods_type');
                $id=I('get.id');
                $types=$type->where("gc_id=$id")->select();
                $this->assign('list',$types);
                $this->id=$id;

                $this->display('Admin/Category/stdmodeList');
        }
    }
    private function _stdmodeAdd(){
        $id=I('get.id');
        $this->id=$id;
        if( IS_GET ){
            $this->display('Admin/Category/stdmodeOperation');
        }else{
            $name=I('post.type_name');
            if($name==''){
                $this->error('请填写物品名称');
            }
            $id=I('post.id');
            $data['gc_id']=$id;
            $data['type_name']=$name;
            $type=M('goods_type');
            $mark=$type->add($data);
            if($mark){
                $this->redirect('Admin/Category/stdmode/id/'.$id);
            }else{
                $this->error('添加失败');
            }
        }

    }
    private function _stdmodeEdit(){
        $type=M('goods_type');
        if( IS_GET ){
            $id=I('get.id');
            $this->id=$id;
            $this->mark=$type->where("type_id=$id")->find();
            $this->display('Admin/Category/stdmodeOperation');
        }else{
            $id=I('post.id');
            $data['type_name']=I('post.type_name');
            $mark=$type->where("type_id=$id")->save($data);
            if($mark){
                $this->success('Admin/Category/stdmode');
            }else{
                $this->error('删除失败');
            }
        }
    }
    private function _stdmodeDel(){
        $id=I('get.id');
        $type=M('goods_type');
        $mark=$type->where("type_id=$id")->delete();
        if($mark){
            $this->success('Admin/Category/stdmode');
        }else{
            $this->error('删除失败');
        }
    }
}
